/*
 * http://www.cde.org.cn/zdyz.do?method=initValue&frameStr=0#top
 * @Author: Duyb
 * @Date: 2021-04-25 16:53:32
 * @Last Modified by: Duyb
 * @Last Modified time: 2021-04-27 22:42:07
 */

var fs = require('fs');
var superagent = require('superagent');
var cheerio = require('cheerio');

const CDE_HOST = 'http://www.cde.org.cn/';

const HOST = 'http://127.0.0.1:5500/source/cde/';

const BASE_URL = `${HOST}zdyz.do?method=initValue&frameStr=0#top`;
const BASE_PATH = './out/cde/20210427/';

const list = [
  {
    type: '征求意见',
    link: 'zdyz.do?method=list&fclass=FCLASS_ZQYJ&',
  },
  { type: '化学药物', link: 'zdyz.do?method=list&fclass=FCLASS_HXYW&' },
  {
    type: '中药、天然药物',
    link: 'zdyz.do?method=list&fclass=FCLASS_ZY_TRYW&',
  },
  { type: '生物制品', link: 'zdyz.do?method=list&fclass=FCLASS_SWZP&' },
  { type: '综合学科', link: 'zdyz.do?method=list&fclass=FCLASS_ZHXK&' },
  { type: '审评一般原则', link: 'zdyz.do?method=list&fclass=FCLASS_SPYZ&' },
  {
    type: '技术标准、技术要求',
    link: 'zdyz.do?method=list&fclass=FCLASS_ZSBZ&',
  },
  { type: '非临床研究', link: 'zdyz.do?method=list&fclass=FCLASS_FLCYJ&' },
];

// const LIST = []; // [{ type, id, link, list[] }]

const LIST = [
  {
    type: '征求意见',
    link:
      'http://www.cde.org.cn/zdyz.do?method=list&fclass=FCLASS_ZQYJ&pageMaxNumber=10000',
  },
  {
    type: '化学药物',
    link:
      'http://www.cde.org.cn/zdyz.do?method=list&fclass=FCLASS_HXYW&pageMaxNumber=10000',
  },
  {
    type: '中药、天然药物',
    link:
      'http://www.cde.org.cn/zdyz.do?method=list&fclass=FCLASS_ZY_TRYW&pageMaxNumber=10000',
  },
  {
    type: '生物制品',
    link:
      'http://www.cde.org.cn/zdyz.do?method=list&fclass=FCLASS_SWZP&pageMaxNumber=10000',
  },
  {
    type: '综合学科',
    link:
      'http://www.cde.org.cn/zdyz.do?method=list&fclass=FCLASS_ZHXK&pageMaxNumber=10000',
  },
  {
    type: '审评一般原则',
    link:
      'http://www.cde.org.cn/zdyz.do?method=list&fclass=FCLASS_SPYZ&pageMaxNumber=10000',
  },
  {
    type: '技术标准、技术要求',
    link:
      'http://www.cde.org.cn/zdyz.do?method=list&fclass=FCLASS_ZSBZ&pageMaxNumber=10000',
  },
  {
    type: '非临床研究',
    link:
      'http://www.cde.org.cn/zdyz.do?method=list&fclass=FCLASS_FLCYJ&pageMaxNumber=10000',
  },
];

let count = 0;
let asList = []; // { type, title, url, typeUrl }

function init(item, i) {
  const fileEncode = encodeURIComponent(`${i + 1}-${item.type}`);
  const url = `${HOST}${fileEncode}.html`;
  console.log('url :', i + 1, item.type, url);
  superagent.get(url).end(function (err, pres) {
    var $ = cheerio.load(pres.text);

    console.log('pres.text :', pres.text);
    // console.log(' --- $(body) :', $('body'));
    console.log('tr length :', $('table tr').length);

    item.list = [];
    const list = item.list;
    $('table [width="700"] tr').each(function () {
      const $tds = $(this).find('td');
      list.push({
        title: $tds.eq(1).text().replace(/\r\n/g, '').trim(),
        url: CDE_HOST + $tds.eq(1).find('a').attr('href'),
        date: $tds.eq(2).text().replace(/\r\n/g, '').trim(),
        step: $tds.eq(3).text().replace(/\r\n/g, '').trim(),
        type: item.type,
      });
    });

    console.log('list :', list);
    count += 1;

    // todo
    if (count !== LIST.length) return;
    writeFile2JSON(BASE_PATH + 'cde-LIST.json', JSON.stringify(LIST));
    asList = LIST.map((item) => item.list).flat();
    writeFile2JSON(BASE_PATH + 'cde-as-LIST.json', JSON.stringify(asList));
  });
}

function writeFile2JSON(path, data) {
  try {
    fs.writeFileSync(path, data);
  } catch (error) {
    throw error;
  }
}

function start() {
  LIST.forEach((item, i) => {
    init(item, i);
  });
}

start();

function json2Md() {
  const input = require('./out/cde/20210427/cde-LIST.json');
}
